<template>
  <el-dialog title="添加条件" :close-on-click-modal="false" width="60%" append-to-body :visible.sync="visibles" @before-close="cancel">
    <el-form :model="dataForm" ref="dataForm" :rules="rules">
      <!-- 条件为方法，左边选择属性-->
      <el-row v-if="dataForm.conditionType == '2'">
        <el-col :span="24">
          <el-form-item label="方法" label-width="150px" prop="">
            <el-col :span="6">
              <el-select v-model="dataForm.leftEntityId" filterable clearable placeholder="请选择模型">
                <el-option v-for="item in entityInfos" :key="item.entityId" :label="item.entityName" :value="item.entityId" @click.native="changeEntityItem(item)">
                </el-option>
              </el-select>
            </el-col>
            <el-col v-if="methodList.length" :span="6">
              <el-select v-model="dataForm.methodName" filterable clearable placeholder="请选择方法">
                <el-option v-for="item in methodList" :key="item.name" :label="item.className" :value="item.name" @click.native="insertM(item)">
                </el-option>
              </el-select>
            </el-col>
          </el-form-item>
        </el-col>
      </el-row>

      <!-- 条件为方法，输入方法参数值-->
      <el-row v-if="dataForm.conditionType == '2' && showParams" style="padding: 10px 0">
        <el-col :span="6" v-for="(p, index) in methodParams" :key="index">
          <el-form-item :label="p.desc" label-width="150px" prop="">
            <el-input v-model="p.className" placeholder="请输入内容"></el-input>
          </el-form-item>
        </el-col>
      </el-row>

      <!-- 条件为属性，左右两边都选择属性-->
      <el-row v-if="dataForm.conditionType == '1'">
        <el-col :span="24">
          <el-form-item label="属性" label-width="150px" prop="">
            <el-col :span="5">
              <el-select v-model="dataForm.leftEntityId" filterable clearable placeholder="请选择模型">
                <el-option v-for="item in entityInfos" :key="item.entityId" :label="item.entityName" :value="item.entityId" @click.native="changeLeftEntity(item)">
                </el-option>
              </el-select>
            </el-col>
            <el-col :span="5">
              <el-select v-model="dataForm.leftProperty" filterable clearable placeholder="请选择属性">
                <el-option v-for="item in leftEntityItemInfos" :key="item.itemId" :label="item.label" :value="item.itemId" @click.native="insertV1(item, '1')">
                </el-option>
              </el-select>
            </el-col>
            <el-col :span="4">
              <el-select v-model="dataForm.sysbol" filterable clearable placeholder="逻辑符号">
                <el-option v-for="item in sysbols" :key="item.value" :label="item.zh" :value="item.value" @click.native="insertV1(item, '2')">
                </el-option>
              </el-select>
            </el-col>
            <el-col :span="5">
              <el-select v-model="dataForm.rightEntityId" filterable clearable placeholder="请选择模型">
                <el-option v-for="item in entityInfos" :key="item.entityId" :label="item.entityName" :value="item.entityId" @click.native="changeRightEntity(item)">
                </el-option>
              </el-select>
            </el-col>
            <el-col :span="5">
              <el-select v-model="dataForm.rightProperty" filterable clearable placeholder="请选择属性">
                <el-option v-for="item in rightEntityItemInfos" :key="item.itemId" :label="item.label" :value="item.itemId" @click.native="insertV1(item, '3')">
                </el-option>
              </el-select>
            </el-col>
          </el-form-item>
        </el-col>
      </el-row>

      <!-- 条件为值，左边选择属性，右边可输入值，或选择枚举值-->
      <el-row v-if="dataForm.conditionType == '3'">
        <el-col :span="24" class="mr10">
          <el-form-item label="模型值" label-width="150px" prop="">
            <el-col :span="5">
              <el-select v-model="dataForm.leftEntityId" filterable clearable placeholder="请选择模型">
                <el-option v-for="item in entityInfos" :key="item.entityId" :label="item.entityName" :value="item.entityId" @click.native="changeLeftEntity(item)">
                </el-option>
              </el-select>
            </el-col>
            <el-col :span="5">
              <el-select v-model="dataForm.leftProperty" filterable clearable placeholder="请选择属性">
                <el-option v-for="item in leftEntityItemInfos" :key="item.itemId" :label="item.label" :value="item.itemId" @click.native="insertV1(item, '1')">
                </el-option>
              </el-select>
            </el-col>
            <el-col :span="4">
              <el-select v-model="dataForm.sysbol" filterable clearable placeholder="逻辑符号">
                <el-option v-for="item in sysbols" :key="item.value" :label="item.zh" :value="item.value" @click.native="insertV1(item, '2')">
                </el-option>
              </el-select>
            </el-col>
            <el-col :span="5">
              <el-input v-model="dataForm.rightValue" @blur="insertV3" placeholder="值"></el-input>
            </el-col>
          </el-form-item>
        </el-col>
      </el-row>

      <!-- 条件为已定义条件-->
      <el-row v-if="dataForm.conditionType == '4'">
        <el-col :span="24" class="mr10">
          <el-form-item label="条件因子" label-width="150px" prop="divisorId">
            <el-col :span="8">
              <el-select v-model="dataForm.divisorId" :diabled="disabled" filterable clearable placeholder="请选择条件因子">
                <el-option v-for="item in conditionDivisorList" :key="item.id" :label="item.divisorName" :value="item.id" @click.native="changeDivisor(item)">
                </el-option>
              </el-select>
            </el-col>

            <!-- 规则因子 非方法 且文本类型 则是输入框-->
            <!--            <el-col :span="5" v-if="divisor.divisorType==='0' && divisor.itemValueType==='1' && (divisor.itemTxtType==='1' || divisor.itemTxtType==='3')">-->
            <!--              <el-input v-model="dataForm.rightValue" @blur="insertV4" placeholder="值"></el-input>-->
            <!--            </el-col>-->
            <!-- 规则因子 非方法 且文本类型 则是日期输入框-->
            <!--            <el-col :span="5" v-else-if="divisor.divisorType==='0' && divisor.itemValueType==='1' && (divisor.itemTxtType==='1' || divisor.itemTxtType==='3')">-->
            <!--              <el-date-picker-->
            <!--                v-model="dataForm.rightValue"-->
            <!--                type="date"-->
            <!--                placeholder="选择日期"-->
            <!--                @blur="insertV5">-->
            <!--              </el-date-picker>-->
            <!--            </el-col>-->
            <!-- 规则因子 非方法 且枚举类型 则是下拉框-->
            <!--            <el-col :span="5" v-else-if="divisor.divisorType==='0' && divisor.itemValueType==='2'">-->
            <!--              <el-select v-model="dataForm.rightValue" filterable clearable placeholder="请选择条件">-->
            <!--                <el-option-->
            <!--                  v-for="item in divisorEnumList"-->
            <!--                  :key="item.enumValue"-->
            <!--                  :label="item.enumCode"-->
            <!--                  :value="item.enumValue"-->
            <!--                @click.native="insertV2(item)">-->
            <!--                </el-option>-->
            <!--              </el-select>-->
            <!--            </el-col>-->
          </el-form-item>
        </el-col>
      </el-row>
      <!-- <el-row v-if="dataForm.conditionType=='4' && divisor.divisorType==='1' && divisorMethodParams.length>0">
        <el-col :span="6" v-for="p in divisorMethodParams">
          <el-form-item :label="p.desc" label-width="150px" prop="">
            <el-input v-model="p.className"  placeholder="值" @blur="changeDivisorParams(p)"></el-input>
          </el-form-item>
        </el-col>
      </el-row> -->

      <el-row>
        <el-col :span="12">
          <el-form-item label="排序" label-width="150px" prop="">
            <el-input v-model="dataForm.seq" placeholder="排序值"></el-input>
          </el-form-item>
        </el-col>
        <!-- <el-col :span="12">
          <el-form-item label="规则定义是否显示" label-width="150px" prop="">
            <el-select
              v-model="dataForm.isShowOnRisk"
              filterable
              clearable
              placeholder="请选择"
            >
              <el-option
                v-for="item in flag"
                :key="item.value"
                :label="item.label"
                :value="item.value"
                @click.native.prevent="changeisShowOnRisk(dataForm,item)"
              >
              </el-option>
            </el-select>
          </el-form-item>
        </el-col> -->
      </el-row>
      <!-- <el-row>
        <el-col :span="12" v-if="isShowOnRisk">
          <el-form-item label="险种是否多选" label-width="150px" prop="">
            <el-select
              v-model="dataForm.isCrtableMore"
              filterable
              clearable
              placeholder="请选择"
            >
              <el-option
                v-for="item in flag"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              >
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12" v-if="isShowOnRisk">
          <el-form-item label="是否关联险种代码" label-width="150px" prop="">
            <el-select
              v-model="dataForm.isRelatetionCrtable"
              filterable
              clearable
              placeholder="请选择"
            >
              <el-option
                v-for="item in flag"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              >
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>
      </el-row> -->

      <!--非方法且非固定值 ITEM_VALUE_TYPE 1-文本，2-枚举 3-模型-->
      <el-row v-if="divisor.divisorType === '0' && divisor.valueType === '2'">
        <el-col :span="12" v-if="divisor.itemValueType === '1'">
          <el-form-item label="默认值" label-width="150px" prop="">
            <el-input v-model="dataForm.rightValue" placeholder="默认值"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12" v-if="divisor.itemValueType === '2'">
          <el-form-item label="默认值" label-width="150px" prop="">
            <el-select v-model="dataForm.rightValue" filterable clearable placeholder="请选择枚举值">
              <el-option v-for="i in divisorEnumList" :key="i.enumCode" :label="i.label" :value="i.enumCode">
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12" v-if="divisor.itemValueType === '3'">
          <el-form-item label="默认模型" label-width="150px" prop="">
            <el-select v-model="dataForm.rightEntityId" filterable clearable placeholder="请选择模型">
              <el-option v-for="i in entityInfos" :key="i.entityId" :label="i.entityName" :value="i.entityId" @click.native="changeRightEntity(i)">
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12" v-if="divisor.itemValueType === '3'">
          <el-form-item label="默认属性" label-width="150px" prop="">
            <el-select v-model="dataForm.rightProperty" filterable clearable placeholder="请选择属性">
              <el-option v-for="i in rightEntityItemInfos" :key="i.itemId" :label="i.label" :value="i.itemId">
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>

      <!--方法-->
      <el-row v-if="divisor.divisorType === '1' && divisorMethodParams">
        <el-col :span="12" v-for="(p, index) in divisorMethodParams" :key="index">
          <el-form-item :label="'默认:' + p.desc" v-if="p.valueType === '2'" label-width="150px" prop="">
            <el-select v-model="p.name" filterable placeholder="值类型" clearable>
              <el-option v-for="item in divisormethodEnum[index]" :key="item.value" :label="item.label" :value="item.value">
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item v-else :label="'默认:' + p.desc" label-width="150px" prop="">
            <el-input v-model="p.name" placeholder="值" @blur="changeDivisorParams(p)"></el-input>
          </el-form-item>
        </el-col>
      </el-row>

      <el-row>
        <el-col :span="24">
          <el-form-item label="表达式" label-width="150px" prop="conditionExpression">
            <el-input type="textarea" :rows="3" :diabled="disabled" v-model="dataForm.conditionExpression"></el-input>
          </el-form-item>
        </el-col>

        <el-col :span="24">
          <el-form-item label="描述" label-width="150px" prop="conditionDesc">
            <el-input type="textarea" :rows="3" :diabled="disabled" v-model="dataForm.conditionDesc"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>

    <span slot="footer" class="dialog-footer">
      <el-button @click="cancel()">取消</el-button>
      <el-button type="primary" @click="getExp()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import {
  entityItemList,
  getEntityMethod,
  entityList,
} from "@/api/baseConfig/entity";
import constant from "../../../../../api/constant";
import {
  listConditionDivisorData,
  getData,
  getEnum,
} from "@/api/baseConfig/conditionDivisor";

export default {
  data() {
    return {
      disabled: false,
      isShowOnRisk: true,
      flag: [
        { label: "否", value: "0" },
        { label: "是", value: "1" },
      ],
      rules: {
        conditionExpression: [
          { required: true, message: "请输入表达式", trigger: "blur" },
        ],
        divisorId: [
          { required: true, message: "请选择条件因子" },
        ],
        conditionDesc: [
          { required: true, message: "描述不能为空", trigger: "blur" },
        ],
      },
      newConditionList: [],
      divisor: {
        configDate: null,
        configUser: null,
        creTime: null,
        creUserId: null,
        divisorIdentify: null,
        divisorMethod: null,
        divisorName: null,
        divisorType: null,
        entityId: null,
        id: null,
        isEffect: null,
        itemId: null,
        itemIdentify: null,
        itemTxtLength: null,
        itemTxtType: null,
        itemValueType: null,
        methodDesc: null,
        methodExpression: null,
        methodParams: null,
        methodType: null,
        remark: null,
        sysbol: null,
      },
      divisorEnumList: [],
      divisormethodEnum: [], //方法参数
      divisorMethodParams: [],
      descData: [],
      dataForm: {
        conditionType: "4", //默认因子
        divisorId: null,
        divisorIdentify: null,
        leftProperty: null,
        rightProperty: null,
        leftEntityId: null,
        rightEntityId: null,
        sysbol: "",
        rightValue: "",
        conditionExpression: "",
        conditionDesc: "",
        conditionMethodType: "",
        methodParams: "",
        methodName: "",
        remark: "",
        seq: null,
        isRelatetionCrtable: null,
        isCrtableMore: null,
        isShowOnRisk: null,
        defaultValue: null,
      },
      showParams: false,
      methodParams: [],
      ruleInfo: {},
      ruleCode: "",
      delInfo: [],
      inputRemark: "",
      rightValue: "",
      curEntity: {},
      methodList: [],
      entityEnum: "",
      enumList: [],
      enumFlag: false,
      entity: null,
      entityItem: null,
      ruleId: null,
      inputV: "",
      listData: [],
      entityInfos: [],
      entityItemInfos: [],
      leftEntityItemInfos: [],
      rightEntityItemInfos: [],
      visibles: false,
      dialogVisible: false,
      sysbols: constant.ruleConfig.sysbols,
      conditionTyps: constant.ruleConfig.conditionTyps,
      conditionDivisorList: [],
    };
  },
  props: {
    visible: {
      type: Boolean,
      default: false,
    },
    conditionList: {
      type: Array, //指定传入的类型
      default: [], //这样可以指定默认的值
    },
    entityList: {
      type: Array, //指定传入的类型
      default: [], //这样可以指定默认的值
    },
    chooseIndex: {
      type: Number,
      default: 0,
    },
    isconflict: {
      type: Boolean,
    },
  },
  created() {
  },
  methods: {
    cancel() {
      this.$emit("cancel", false);
      this.visibles = this.visible;
    },
    changeisShowOnRisk(dataform, item) {
      // console.log(dataform,item,'22');
      // 1：是，2：否
      if (item.value === '1') {
        this.isShowOnRisk = true
      } else {
        this.isShowOnRisk = false
        this.dataForm.isCrtableMore = ''
        this.dataForm.isRelatetionCrtable = ''
      }
    },
    changeDivisorParams(data) {
      // console.log(data);
    },
    // 选择条件因子
    changeDivisor(data) {
      // console.log(data, "");
      this.divisor = data;
      this.divisorMethodParams = [];
      this.divisorEnumList = [];
      // this.divisormethodEnum = []
      this.descData = new Array(2);

      //判断因子类型是方法还是非方法
      if (data.divisorType === "0") {
        //非方法 枚举
        if (data.itemValueType === "2") {
          getData({ id: data.id }).then((res) => {
            let arr = [];
            res.data.data.enumList.forEach((item) => {
              item["label"] = item.enumCode + "--" + item.enumValue;
              arr.push(item);
            });
            this.divisorEnumList = arr;
            // console.log(this.divisorEnumList);
            this.$forceUpdate();
          });
        }
        let desc = JSON.parse(data.remark);
        let s =
          desc[1].k.substring(0, 1).toLocaleUpperCase() +
          desc[1].k.substring(1, desc[1].k.length);

        let v = "$" + desc[0].k + ".get" + s + "() " + desc[2].k;
        let k = desc[0].v + "的 " + desc[1].v + " " + desc[2].v;
        this.descData[0] = { k: k, v: v };

        //文本
        if (data.valueType === "1" && data.itemValueType === "1") {
          this.descData[1] = { k: data.rightValue, v: data.rightValue };
        } else {
          if (data.itemValueType === "3") {
            // let k1 = desc[2].k
            // let v1 = desc[2].v
            // this.descData[1] = {k:k1,v:v1}

            if (data.valueType === "1") {
              let s1 =
                desc[4].k.substring(0, 1).toLocaleUpperCase() +
                desc[4].k.substring(1, desc[4].k.length);
              let v1 = "$" + desc[3].k + ".get" + s1 + "() ";
              let k1 = desc[3].v + "的 " + desc[4].v;
              this.descData[1] = { k: k1, v: v1 };
            }
          } else {
            this.descData[1] = {};
          }
        }
        // this.dataForm.rightValue = ''

        //非模型
        if (data.itemValueType != "3") {
          this.showValue();
        } else {
          this.showValue1();
        }
      } else if (data.divisorType === "1") {
        // 枚举方法
        if (data.divisorType === "1" && data.methodParams) {
          let methodParams = JSON.parse(data.methodParams);
          let divisormethodEnum = [];
          methodParams.forEach((e, i) => {
            divisormethodEnum[i] = [];
            if (e.valueType === "2") {
              let param = {
                divisor_identify: data.divisorIdentify,
                param_name: e.name,
              };
              // debugger
              getEnum(param).then(({ data }) => {
                // console.log(data);
                if (data.resultCode === 0) {
                  data.data.forEach((item) => {
                    item["label"] = item.enumCode + "--" + item.enumValue;
                    item["value"] = item.enumCode;
                    divisormethodEnum[i].push(item);
                  });
                }
              });
            }
          });
          this.$set(this, "divisormethodEnum", divisormethodEnum);
        }
        this.descData[0] = {
          k: data.methodDesc,
          v: "ClmUtil." + data.methodExpression,
        };
        this.dataForm.conditionMethodType = data.methodType;
        this.dataForm.leftEntityId = data.entityId;
        if (data.methodParams) {
          let m = JSON.parse(data.methodParams);
          for (var i = 0; i < m.length; i++) {
            let mp = m[i];
            mp["attri"] = m[i].name;
            if (!mp.desc) {
              mp.desc = "";
            }
            this.divisorMethodParams.push(mp);
          }
        }
        this.dataForm.methodName = data.methodExpression;
        this.showValue1();
      } else if (data.divisorType === "3") {
        // 枚举方法
        if (data.divisorType === "1" && data.methodParams) {
          let methodParams = JSON.parse(data.methodParams);
          let divisormethodEnum = [];
          methodParams.forEach((e, i) => {
            divisormethodEnum[i] = [];
            if (e.valueType === "2") {
              let param = {
                divisor_identify: data.divisorIdentify,
                param_name: e.name,
              };
              // debugger
              getEnum(param).then(({ data }) => {
                // console.log(data);
                if (data.resultCode === 0) {
                  data.data.forEach((item) => {
                    item["label"] = item.enumCode + "--" + item.enumValue;
                    item["value"] = item.enumCode;
                    divisormethodEnum[i].push(item);
                  });
                }
              });
            }
          });
          this.$set(this, "divisormethodEnum", divisormethodEnum);
        }
        this.descData[0] = {
          k: data.methodDesc,
          v: data.methodExpression,
        };
        this.dataForm.conditionMethodType = data.methodType;
        this.dataForm.leftEntityId = data.entityId;
        if (data.methodParams) {
          let m = JSON.parse(data.methodParams);
          for (var i = 0; i < m.length; i++) {
            let mp = m[i];
            mp["attri"] = m[i].name;
            if (!mp.desc) {
              mp.desc = "";
            }
            this.divisorMethodParams.push(mp);
          }
        }
        this.dataForm.methodName = data.methodExpression;
        this.showValue1();
      }

      this.dataForm.conditionDesc = data.divisorName;
      this.dataForm.divisorIdentify = data.divisorIdentify;
    },
    // 获取规则因子,实体类
    initData() {
      let params = {
        is_effect: "1",
      };
      this.conditionDivisorList = [];
      listConditionDivisorData(params).then((res) => {
        let selectArr = [];
        let nonSelectArr = [];
        // debugger
        //筛选非模型的因子
        // console.log(this.isconflict, "222");
        if (this.isconflict) {
          this.conditionDivisorList = res.data.data;
        } else {
          res.data.data.forEach((item) => {
            let flag = false;
            // console.log(this.entityList,'this.entityList');
            this.entityList.forEach((e) => {
              if (e.entityId === item.entityId) {
                //方法
                if (item.divisorType == "1") {
                  flag = true;
                } else if (item.divisorType == "3") {
                  //自定义条件
                  flag = true;
                } else if (
                  item.divisorType == "0" &&
                  item.itemValueType != "3"
                ) {
                  //非方法且不为模型类型
                  flag = true;
                }
              }
            });
            if (flag) {
              selectArr.push(item);
            } else {
              if (item.itemValueType === "3") {
                nonSelectArr.push(item);
              }
            }
          });
          //筛选模型因子
          if (nonSelectArr.length > 0) {
            nonSelectArr.forEach((item) => {
              let flag1 = false;
              let flag2 = false;
              this.entityList.forEach((e) => {
                if (e.entityId === item.entityId) {
                  flag1 = true;
                }
                if (
                  item.valueType === "1" &&
                  e.entityId === item.rightEntityId
                ) {
                  flag2 = true;
                }
              });
              if (item.valueType === "1") {
                if (flag1 && flag2) {
                  this.conditionDivisorList.push(item);
                }
              } else {
                if (flag1) {
                  this.conditionDivisorList.push(item);
                }
              }
            });
          }

          selectArr.forEach((item) => {
            this.conditionDivisorList.push(item);
          });
          // console.log(nonSelectArr);
          // console.log(selectArr);
        }
        // console.log(this.conditionDivisorList);

        if (this.dataForm.divisorId) {
          this.conditionDivisorList.forEach((item) => {
            if (item.id === this.dataForm.divisorId) {
              this.divisor = item;
              // this.changeDivisor(item)
            }
          });
          if (this.dataForm.conditionType === "4") {
            if (
              this.divisor.divisorType === "1" &&
              this.dataForm.methodParams
            ) {
              //方法
              this.divisorMethodParams = JSON.parse(this.dataForm.methodParams);
            }
          }
          getData({ id: this.dataForm.divisorId }).then((res) => {
            let arr = [];
            res.data.data.enumList.forEach((item) => {
              item["label"] = item.enumCode + "--" + item.enumValue;
              arr.push(item);
            });
            this.divisorEnumList = arr;
            // console.log(this.divisorEnumList);
            this.$forceUpdate();
          });
        }
      });
      if (this.entityList.length > 0) {
        this.entityInfos = this.entityList;
      } else {
        entityList(params).then((res) => {
          this.entityInfos = res.data.data;
        });
      }

      if (this.dataForm.rightEntityId) {
        let params1 = {
          id: this.dataForm.rightEntityId + "",
        };
        // 获取实体类属性
        entityItemList(params1).then((res) => {
          this.rightEntityItemInfos = []; // res.data.data
          if (res.data.data) {
            res.data.data.forEach((item) => {
              item["label"] = item.itemName + "--" + item.itemIdentify;
              this.rightEntityItemInfos.push(item);
            });
          }
        });
      }
      if (this.dataForm.conditionType === "2") {
        if (this.dataForm.methodParams) {
          this.methodParams = JSON.parse(this.dataForm.methodParams);
          if (this.methodParams.length > 0) {
            this.showParams = true;
          } else {
            this.showParams = false;
          }
        }

        let params1 = {
          id: this.dataForm.leftEntityId + "",
        };

        // 获取方法
        getEntityMethod(params1).then((res) => {
          this.methodList = res.data.data;
        });
      } else if (this.dataForm.conditionType === "1") {
        //属性
        let params = {
          id: this.dataForm.leftEntityId + "",
        };
        // 获取实体类属性
        this.leftEntityItemInfos = [];
        entityItemList(params).then((res) => {
          if (res.data.data) {
            res.data.data.forEach((item) => {
              item["label"] = item.itemName + "--" + item.itemIdentify;
              this.leftEntityItemInfos.push(item);
            });
          }
        });
        let params1 = {
          id: this.dataForm.rightEntityId + "",
        };
        // 获取实体类属性
        entityItemList(params1).then((res) => {
          this.rightEntityItemInfos = []; // res.data.data
          if (res.data.data) {
            res.data.data.forEach((item) => {
              item["label"] = item.itemName + "--" + item.itemIdentify;
              this.rightEntityItemInfos.push(item);
            });
          }
        });
      } else if (this.dataForm.conditionType === "3") {
        //属性值
        let params = {
          id: this.dataForm.leftEntityId + "" + "",
        };
        // 获取实体类属性
        this.leftEntityItemInfos = [];
        entityItemList(params).then((res) => {
          if (res.data.data) {
            res.data.data.forEach((item) => {
              item["label"] = item.itemName + "--" + item.itemIdentify;
              this.leftEntityItemInfos.push(item);
            });
          }
        });
      }
    },

    // 选择条件类型
    changeConditionType(item) {
      // console.log(item);
      this.dataForm.conditionType = item.value;
      // 属性
      if (this.dataForm.conditionType === "1") {
        this.descData = new Array(5);
      } else if (this.dataForm.conditionType === "3") {
        // 模型值
        this.descData = new Array(4);
      } else if (this.dataForm.conditionType === "4") {
        // 已定义条件
        this.descData = new Array(2);
      }

      this.inputV = "";
      this.inputRemark = "";
      this.dataForm.sysbol = "";
      this.dataForm.rightValue = "";
      this.dataForm.conditionMethodType = "";
      this.dataForm.leftProperty = null;
      this.dataForm.rightProperty = null;
      this.dataForm.leftEntityId = null;
      this.dataForm.rightEntityId = null;
      this.dataForm.divisorId = null;
      this.methodParams = [];
      this.methodList = [];
      this.showParams = false;
      this.dataForm.conditionExpression = "";
      this.dataForm.conditionDesc = "";
    },
    // 表达式填写完毕 确定
    getExp() {
      // console.log(this.dataForm);
      let that = this;
      this.$refs["dataForm"].validate((valid) => {
        if (valid) {
          // alert('submit!');
          let inputValue = that.dataForm.methodName;
          that.dataForm.defaultValue = "";
          // if(!this.dataForm.conditionType){
          //     this.$message.error('请选择条件类型' )
          //     return
          // }
          if (that.dataForm.conditionType === "1") {
            //模型属性
            if (
              !this.dataForm.leftEntityId ||
              !this.dataForm.leftProperty
              // || !this.dataForm.sysbol || !this.dataForm.rightEntityId || !this.dataForm.rightProperty
            ) {
              this.$message.error("请将模型值补充完整");
              return;
            }
          } else if (
            that.dataForm.conditionType === "2" &&
            that.methodList.length
          ) {
            if (!this.dataForm.leftEntityId || !this.dataForm.methodName) {
              this.$message.error("请将方法补充完整");
              return;
            }
            //方法
            if (this.showParams && this.methodParams) {
              for (var i = 0; i < this.methodParams.length; i++) {
                let item = this.methodParams[i];
                let value = item.name;
                // if (!value) {
                //     this.$message.error('请输入' + item.desc)
                //     return
                // }
                if (item.type === "int" && value) {
                  const digit = /^[0-9]*$/;
                  if (!digit.test(value)) {
                    this.$message.error(item.desc + "只能输入数字");
                    return;
                  }
                } else if (item.type === "String" && value) {
                  value = '"' + value + '"';
                }
                if (value) {
                  let p = "$" + item.name + "$";
                  inputValue = inputValue.replace(p, value);
                }
              }
              this.dataForm.methodParams = JSON.stringify(this.methodParams);
            }
            this.dataForm.conditionExpression = "ClmUtil." + inputValue;
          } else if (that.dataForm.conditionType === "3") {
            //模型值
            if (
              !this.dataForm.leftEntityId ||
              !this.dataForm.leftProperty
              // || !this.dataForm.sysbol || !this.dataForm.rightValue
            ) {
              this.$message.error("请将模型值补充完整");
              return;
            }
          } else if (that.dataForm.conditionType === "4") {
            //已定义条件
            if (!that.dataForm.divisorId) {
              that.$message.error("请将条件补充完整");
              return;
            }
            if (that.divisor.divisorType === "1") {
              //方法
              // console.log(that.divisorMethodParams);
              if (
                that.divisorMethodParams &&
                that.divisorMethodParams.length > 0
              ) {
                let flag = false;
                for (var i = 0; i < that.divisorMethodParams.length; i++) {
                  let param = that.divisorMethodParams[i];
                  let newValue = param["name"];
                  let oldValue = param["attri"];
                  // if (!newValue) {
                  //   that.divisorMethodParams[i]["name"] = oldValue;
                  // }
                  // if (newValue != oldValue) {
                  //   //如果参数有改变
                  //   flag = true;
                  // }

                  // if (!newValue && flag) {
                  //   that.$message.error("请输入" + param.desc);
                  //   return;
                  // }
                  if (param["type"] === "int" && newValue) {
                    const digit = /^[0-9]*$/;
                    if (!digit.test(newValue)) {
                      that.$message.error(param.desc + "只能输入数字");
                      return;
                    }
                  } else if (param["type"] === "String" && newValue) {
                    newValue = '"' + newValue + '"';
                  }
                  if (newValue) {
                    let p = "$" + param.name + "$";
                    inputValue = inputValue.replace(p, newValue);
                  }
                }
                that.dataForm.methodParams = JSON.stringify(
                  that.divisorMethodParams
                );
                if (
                  that.divisor.methodType === "0" ||
                  that.divisor.methodType === "1" ||
                  that.divisor.methodType === "2"
                ) {
                  that.dataForm.conditionMethodType = "0";
                } else if (this.divisor.methodType === "3") {
                  that.dataForm.conditionMethodType = "1";
                }
              }
              if (!that.dataForm.conditionExpression) {
                that.dataForm.conditionExpression = "ClmUtil." + inputValue;
              }
              that.dataForm.defaultValue = "ClmUtil." + inputValue;
              that.dataForm.defaultValue = "";
            } else {
              this.dataForm.leftEntityId = this.divisor.entityId;
              this.dataForm.leftProperty = this.divisor.itemId;
              this.dataForm.sysbol = this.divisor.sysbol;
              if (this.dataForm.rightValue) {
                // this.$message.error('请将条件值补充完整' )
                // return
                that.dataForm.defaultValue =
                  that.dataForm.conditionExpression + this.dataForm.rightValue;
              }
              // if(this.divisor.itemValueType==='1' && this.dataForm.rightValue.length>this.divisor.itemTxtLength){
              //     this.$message.error('条件值长度不能大于'+this.divisor.itemTxtLength )
              //     return
              // }
            }
          }

          that.dataForm.remark = JSON.stringify(that.descData);
          // that.visible = false;
          // if(this.chooseIndex!=null){
          //     this.newConditionList[this.chooseIndex] = this.dataForm
          // }else{
          //     this.newConditionList.push(this.dataForm)
          // }
          that.dataForm.isEffect = "1";
          // console.log(that.dataForm, that.divisor, "methodList");
          if (that.methodList.length && that.methodList[0].params > 1) {
            that.dataForm.params = that.methodList[0].params;
          }
          that.$emit("refreshDataList", that.dataForm);
          that.cancel();
        } else {
          // console.log('error submit!!');
          return false;
        }
      });
    },

    // 选择方法
    insertM(data) {
      // console.log(data, "data");
      this.dataForm.conditionExpression = "ClmUtil." + data.name;
      this.dataForm.methodName = data.name;
      this.dataForm.conditionDesc = data.className;
      this.showParams = false;
      // 有参数
      if (data.type === "1" || data.type === "3") {
        if (data.params) {
          this.showParams = true;
          this.methodParams = data.params;
        } else {
          this.showParams = false;
          this.methodParams = [];
        }
      }
      if (data.type === "0" || data.type === "1" || data.type === "2") {
        this.dataForm.conditionMethodType = "0";
      } else {
        this.dataForm.conditionMethodType = "1";
      }
      // console.log(this.dataForm.conditionMethodType, this.showParams);
    },

    // 显示描述信息
    showValue() {
      this.dataForm.conditionExpression = "";
      this.dataForm.conditionDesc = "";
      if (this.dataForm.rightValue == null) {
        this.dataForm.rightValue = "";
      }
      this.descData.forEach((item) => {
        if (item.v && item.k) {
          this.dataForm.conditionExpression =
            this.dataForm.conditionExpression +
            item.v +
            this.dataForm.rightValue;
          this.dataForm.conditionDesc =
            this.dataForm.conditionDesc + item.k + +this.dataForm.rightValue;
          // console.log(this.dataForm.conditionExpression);
        }
      });
    },
    showValue1() {
      this.dataForm.conditionExpression = "";
      this.dataForm.conditionDesc = "";
      if (this.dataForm.rightValue == null) {
        this.dataForm.rightValue = "";
      }
      this.descData.forEach((item) => {
        if (item.v && item.k) {
          this.dataForm.conditionExpression =
            this.dataForm.conditionExpression + item.v;
          this.dataForm.conditionDesc = this.dataForm.conditionDesc + item.k;
          // console.log(this.dataForm.conditionExpression);
        }
      });
    },

    // 属性值选择
    insertV1(data, item) {
      // console.log(data);
      if (item === "1") {
        // 左
        let s =
          data.itemIdentify.substring(0, 1).toLocaleUpperCase() +
          data.itemIdentify.substring(1, data.itemIdentify.length);
        let v = "get" + s + "()";
        this.descData[1] = { k: data.itemName, v: v };

        this.dataForm.leftProperty = data.itemId;
        if (this.conditionType === "3") {
          if (data.enumList && data.enumList.length > 0) {
            this.enumList = data.enumList;
            this.enumFlag = true;
          } else {
            this.enumList = [];
            this.enumFlag = false;
          }
        }
      } else if (item === "2") {
        // 比较符
        this.descData[2] = { k: data.zh, v: data.label };

        this.dataForm.sysbol = data.value;
      } else if (item === "3") {
        // 右

        let s =
          data.itemIdentify.substring(0, 1).toLocaleUpperCase() +
          data.itemIdentify.substring(1, data.itemIdentify.length);
        let v = "get" + s + "()";
        this.descData[4] = { k: data.itemName, v: v };
        this.dataForm.rightProperty = data.itemId;
      }

      this.showValue();
      // console.log(this.dataForm);
    },
    // 条件因子枚举值选择
    insertV2(item) {
      this.descData[2] = { k: item.enumCode, v: this.dataForm.rightValue };
      this.showValue();
    },
    // 输入值
    insertV3() {
      this.descData[3] = {
        k: this.dataForm.rightValue,
        v: this.dataForm.rightValue,
      };
      this.showValue();
    },
    //条件因子值输入
    insertV4() {
      this.descData[1] = {
        k: this.dataForm.rightValue,
        v: this.dataForm.rightValue,
      };
      this.showValue();
    },
    //条件因子日期值输入
    insertV5() {
      this.descData[1] = {
        k: this.dataForm.rightValue,
        v: this.dataForm.rightValue,
      };
      this.showValue();
    },

    init() {
      // console.log('123')
      // this.visible = true;
      this.disabled = false;

      this.newConditionList = [];
      this.divisor = {
        configDate: null,
        configUser: null,
        creTime: null,
        creUserId: null,
        divisorIdentify: null,
        divisorMethod: null,
        divisorName: null,
        divisorType: null,
        entityId: null,
        id: null,
        isEffect: null,
        itemId: null,
        itemIdentify: null,
        itemTxtLength: null,
        itemTxtType: null,
        itemValueType: null,
        methodDesc: null,
        methodExpression: null,
        methodParams: null,
        methodType: null,
        remark: null,
        sysbol: null,
      };
      this.dataForm = {
        rightProperty: null,
        rightEntityId: null,
        sysbol: "",
        rightValue: "",
        remark: "",
        decisionCode: "",
        headType: "1",
        conditionType: "4",
        divisorId: null,
        divisorIdentify: null,
        leftProperty: null,
        leftEntityId: null,
        conditionExpression: "",
        conditionDesc: "",
        conditionMethodType: "",
        methodParams: "",
        methodName: "",
      };
      this.entityInfos = [];
      this.delInfo = [];
      this.methodList = [];
      this.methodParams = [];
      this.divisorMethodParams = [];
      this.showParams = false;
      this.conditionType = "";
      this.descData = [];
      this.newConditionList = JSON.parse(JSON.stringify(this.conditionList));
      // console.log('测试：', this.chooseIndex, this.conditionList.length > 0)
      if (this.chooseIndex != null && this.conditionList.length > 0) {
        this.dataForm = this.newConditionList[this.chooseIndex];
        this.descData = JSON.parse(this.dataForm.remark);
        this.disabled = true;
      } else {
        this.descData = new Array(2);
      }
      // console.log(this.descData,'descData');
      this.initData();
    },

    // 左实体
    changeLeftEntity(item) {
      this.dataForm.leftEntityId = item.entityId;
      this.dataForm.leftProperty = null;

      this.descData[0] = {
        k: item.entityName + " 的",
        v: "$" + item.entityIdentify + ".",
      };
      this.descData[1] = "";

      this.showValue();
      let params = {
        id: item.entityId + "",
      };
      // 获取实体类属性
      this.leftEntityItemInfos = [];
      entityItemList(params).then((res) => {
        if (res.data.data) {
          res.data.data.forEach((item) => {
            item["label"] = item.itemName + "--" + item.itemIdentify;
            this.leftEntityItemInfos.push(item);
          });
        }
        // this.leftEntityItemInfos = res.data.data
      });
    },
    // 右实体
    changeRightEntity(item) {
      this.dataForm.rightEntityId = item.entityId;
      this.dataForm.rightProperty = null;
      // this.descData[3] = {k:item.entityName + ' 的',v:'$' + item.entityIdentify + '.'}
      // this.descData[4] = ''
      // this.showValue()
      let params = {
        id: item.entityId + "",
      };
      // 获取实体类属性
      entityItemList(params).then((res) => {
        this.rightEntityItemInfos = []; // res.data.data
        if (res.data.data) {
          res.data.data.forEach((item) => {
            item["label"] = item.itemName + "--" + item.itemIdentify;
            this.rightEntityItemInfos.push(item);
          });
        }
      });
    },

    changeEntityItem(item) {
      // console.log(item);
      this.methodList = [];
      this.showParams = false;
      this.dataForm.methodName = "";
      this.methodParams = [];
      this.divisorMethodParams = [];
      this.curEntity = item;
      this.dataForm.leftEntityId = item.entityId;
      let params = {
        id: item.entityId + "",
      };
      // 获取实体类属性
      entityItemList(params).then((res) => {
        this.entityItemInfos = res.data.data;
      });
      let params1 = {
        className: item.pkgName,
      };

      // console.log(params1);
      // 获取方法
      getEntityMethod(params1).then((res) => {
        this.methodList = res.data.data;
      });
      this.enumList = [];
      this.enumFlag = false;
    },

    // 表单提交
    dataFormSubmit() {
      if (this.conditionList) {
        for (var i = 0; i < this.conditionList.length; i++) {
          let condition = this.conditionList[i];
          if (!condition.conditionName) {
            this.$message.error("请填写条件名称");
            return;
          }
          if (!condition.conditionExpression) {
            this.$message.error("请填写条件表达式");
            return;
          }
        }
      }
      let params = {
        id: this.ruleId + "",
        relInfo: JSON.stringify(this.conditionList),
        delInfo: JSON.stringify(this.delInfo),
      };
      saveCondition(params).then((res) => {
        if (res.data && res.data.resultCode === 0) {
          this.$message({
            message: "操作成功",
            type: "success",
            duration: 1500,
            onClose: () => {
              // this.visible = false;
              this.$emit("refreshDataList");
            },
          });
        } else {
          this.$message.error(res.data.resultMsg);
        }
      });
    },
  },
  watch: {
    // this.visibles = this.visible;
    visible(newVal, oldVal) {
      if (newVal) {
        this.visibles = this.visible;
      }
    },
    visibles(newVal, oldVal) {
      if (newVal) {
        this.cancel();
      }
    }
  }
};
</script>

<style lang="scss">
.title {
  padding: 10px;
  background-color: rgba(248, 248, 248, 1);
  margin-bottom: 10px;
}

.el-table__row .el-form-item__content {
  margin-left: 0 !important;
}

.el-table__row .el-form-item {
  margin-bottom: 0 !important;
}

.btn-span {
  display: block;
  text-align: center;
  border: 1px solid #dcdfe6;
  border-radius: 5px;
  cursor: pointer;
}

.mr10 {
  margin-right: 10px;
}
</style>
